package com.runen.infinitemovement.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.runen.infinitemovement.Constant;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.activity.MyTheLongMarchRoadActivity;
import com.runen.infinitemovement.activity.WeTheLongMarchRoadActivity;
import com.runen.infinitemovement.activity.WebActivity;
import com.runen.infinitemovement.adapter.MyActivityAdapter;
import com.runen.infinitemovement.model.api.ActivityAPI;
import com.runen.infinitemovement.model.api.UserAPI;
import com.runen.infinitemovement.model.entity.ActivitiesEntity;
import com.runen.infinitemovement.model.entity.MyEnterActivityies;
import com.runen.infinitemovement.widget.CustomLoadMoreView;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyActivityFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.rv_activity)
    RecyclerView rvActivity;
    @BindView(R.id.srl)
    SwipeRefreshLayout srl;
    private int flag;
    private View view;
    private MyActivityAdapter myActivityAdapter;
    private int page = 2;

    public MyActivityFragment() {
    }

    public MyActivityFragment(int position) {
        flag = position;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.my_activity_fragment, container, false);
            ButterKnife.bind(this, view);
            initView();
        }
        return view;
    }

    private void initView() {
        srl.setOnRefreshListener(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        rvActivity.setLayoutManager(layoutManager);
        myActivityAdapter = new MyActivityAdapter(getActivity());
        myActivityAdapter.getLoadMoreModule().setLoadMoreView(new CustomLoadMoreView());
        rvActivity.setAdapter(myActivityAdapter);

        initData();

        myActivityAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (myActivityAdapter.getData().get(position).activeType == 0) {
                    Intent intent = new Intent(getActivity(), WeTheLongMarchRoadActivity.class);
                    startActivity(intent);
                } else if (myActivityAdapter.getData().get(position).activeType == 1) {
                    Intent intent = new Intent(getActivity(), MyTheLongMarchRoadActivity.class);
                    intent.putExtra("userId", UserAPI.getUserId());
                    startActivity(intent);
                } else {
                    Intent intent1 = new Intent(getActivity(), WebActivity.class);
                    intent1.putExtra("LINK_URL", String.format(Constant.ACTIVITY_DETAILS_H5,
                            myActivityAdapter.getData().get(position).activeId, UserAPI.getUserToken()));
                    startActivity(intent1);
                }
            }
        });
        initLoadMore();
    }

    private void initLoadMore() {
        myActivityAdapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                loadMoreRequested();
            }
        });
        myActivityAdapter.getLoadMoreModule().setAutoLoadMore(true);
        myActivityAdapter.getLoadMoreModule().setEnableLoadMoreIfNotFullPage(false);
    }

    @Override
    public void onRefresh() {
        page = 2;
        initData();
    }

    private void initData() {
        srl.setRefreshing(true);
        String string = "?limit=20&page=1";
        new ActivityAPI().getMyEnterOrPublishActivity(string, flag, new BaseResultCallback<MyEnterActivityies>() {
            @Override
            public void onResponse(MyEnterActivityies response) {
                srl.setRefreshing(false);
                if (response.code == 0) {
                    if (response.data != null) {
                        List<ActivitiesEntity> activitiesEntities = new ArrayList<>();
                        if (response.data.myEnterActivitiesDto != null) {
                            activitiesEntities.add(response.data.myEnterActivitiesDto);
                        }
                        if (response.data.myEnterActivitiesDtoList != null && response.data.myEnterActivitiesDtoList.size() > 0) {
                            activitiesEntities.addAll(response.data.myEnterActivitiesDtoList);
                        }
                        myActivityAdapter.setNewData(activitiesEntities);
                        if (myActivityAdapter.getData().size() >= response.data.count) {
                            myActivityAdapter.getLoadMoreModule().loadMoreEnd(true);
                            myActivityAdapter.getLoadMoreModule().setEnableLoadMore(false);
                        }
                    }
                } else {
                    ToastUtils.showShort(response.msg);
                }
            }
        });
    }

    public void loadMoreRequested() {
        String string = "?limit=20&page=%s";
        new ActivityAPI().getMyEnterOrPublishActivity(String.format(string, page), flag, new BaseResultCallback<MyEnterActivityies>() {
            @Override
            public void onResponse(MyEnterActivityies response) {
                if (response.code == 0) {
                    if (response.data != null) {
                        if (response.data.myEnterActivitiesDtoList != null && response.data.myEnterActivitiesDtoList.size() > 0) {
                            myActivityAdapter.addData(response.data.myEnterActivitiesDtoList);
                            if (myActivityAdapter.getData().size() >= response.data.count) {
                                myActivityAdapter.getLoadMoreModule().loadMoreEnd(true);
                                myActivityAdapter.getLoadMoreModule().setEnableLoadMore(false);
                            } else {
                                page++;
                                myActivityAdapter.getLoadMoreModule().loadMoreComplete();
                                myActivityAdapter.getLoadMoreModule().setEnableLoadMore(true);
                            }
                        }

                    }
                } else {
                    ToastUtils.showShort(response.msg);
                }
            }
        });
    }
}
